package com.appkudos.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;
import android.widget.MultiAutoCompleteTextView.Tokenizer;

public class Controller extends Activity {
    /** Called when the activity is first created. */
	/*protected static final String URL_REC_QUERIES = "http://172.16.56.1/appkudos/test-RecControllerQueries.php";
	protected static final String URL_GROUP_QUERIES = "http://172.16.56.1/appkudos/test-GroupControllerQueries.php";
	protected static final String URL_USER_QUERIES = "http://172.16.56.1/appkudos/test-UserControllerQueries.php";
	*/
	protected static final String URL_REC_QUERIES = "http://yangjustine.startlogic.com/appkudos/RecControllerQueries.php";
	protected static final String URL_GROUP_QUERIES = "http://yangjustine.startlogic.com/appkudos/GroupControllerQueries.php";
	protected static final String URL_USER_QUERIES = "http://yangjustine.startlogic.com/appkudos/UserControllerQueries.php";
	
//	protected static final String URL_REC_QUERIES = "http://www.tiffany-ng.com/appkudos/RecControllerQueries.php";
//	protected static final String URL_GROUP_QUERIES = "http://www.tiffany-ng.com/appkudos/GroupControllerQueries.php";
//	protected static final String URL_USER_QUERIES = "http://www.tiffany-ng.com/appkudos/UserControllerQueries.php";
//	
//	protected static final String URL_REC_QUERIES = "http://10.2.24.216/appkudos/test-RecControllerQueries.php";
//	protected static final String URL_GROUP_QUERIES = "http://10.2.24.216/appkudos/test-GroupControllerQueries.php";
//	protected static final String URL_USER_QUERIES = "http://10.2.24.216/appkudos/test-UserControllerQueries.php";
//	
	InputStream is = null;
	String response = "";
	String output = "";
	ArrayList<NameValuePair> nameValuePairs;
	
	/***
	 * Helper method for executing a database insertion/deletion
	 * @param jsonResponse returned from the HTTPPost in sendData
	 * @return True if successfully executed operation, False otherwise
	 */
	protected boolean executeOperation(String jsonResponse){
		//parse json data
	    try{
	    	JSONArray jArray = new JSONArray("["+jsonResponse+"]");
    		for(int i=0;i<jArray.length()-1;i++){
            	JSONObject json_data = jArray.getJSONObject(i);
            	if(json_data.has("ERROR")){
            		return false;
            	}else{
	            	return true;
	            }
            }
	    }catch(JSONException e){
	    		//output[0] = "ERROR: "+e.toString();
	    	return false;
	    }
	    
	   	return true;
	}
	
	/***
	 * Helper function to send and retrieve data from the database
	 * @param url - URL where the PHP file containing database queries is located
	 * @param values - array of REQUEST variables to send to the PHP file
	 * @return	A JSON string of the PHP json-encoded response, e.g. {'username':'Tiffany'}
	 */
	protected String sendData(String url, ArrayList<NameValuePair> values){
		String resultString = "";
		//http post
	    try{
	            HttpClient httpclient = new DefaultHttpClient();          
	            HttpPost httppost = new HttpPost(url);           
	            httppost.setEntity(new UrlEncodedFormEntity(values));            
	            HttpResponse response = httpclient.execute(httppost);	           
	            HttpEntity entity = response.getEntity();
	            is = entity.getContent();	          
	    }catch(Exception e){
	            Log.e("log_tag", "Error in http connection "+e.toString());
	    }
	    
	    //convert response to string
	    try{
	            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line);
	            }
	            	    		
	    		is.close();
	    		
	    		resultString=sb.toString();
	    		resultString=resultString.substring(0, resultString.length()-1);
	    }catch(Exception e){
	            Log.e("log_tag", "Error converting result "+e.toString());
	    }	    
	    
	    return resultString;
	}
	
}